package com.hgkj.cloud.customer.controller;

import com.hgkj.cloud.customer.common.DataGrid;
import com.hgkj.cloud.base.domain.TenantFrameworks;
import com.hgkj.cloud.base.domain.TenantUsers;
import com.hgkj.cloud.base.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/framework")
public class FrameworkController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SysTenantsService sysTenantsService;
    @Autowired
    private TenantFrameworksService tenantFrameworksService;
    @Autowired
    private TenantUsersService tenantUsersService;
    @Autowired
    private EUserService eUserService;
    @Autowired
    private HcChannelsService hcChannelsService;
    @Autowired
    private JdbcTemplate jdbcTemplate;

//    /**
//     * 大租户下租户的树形机构数据
//     * @param ids
//     * @return
//     */
//    @RequestMapping(value = "/choseTenantTree")
//    @ResponseBody
//    public List<TreeGrid> choseTenantTree(String ids,String user_id) {
////        User user = (User) SecurityUtils.getSubject().getPrincipal();
//        TenantUsers user = getUserInfoById(user_id);
//        SysTenants tenant = sysTenantsService.getSimpleTenantById(user.getTenant_id());
//        List<TreeGrid> list = new ArrayList<TreeGrid>();
//        TreeGrid tenantTree = tenantTree(tenant.getId());
//        list.add(tenantTree);
//        return list;
//    }

    /**
     * 问卷任务添加分组获取初始选中组织结构
     * @param dataGrid
     * @param task_id
     * @param framework_id
     * @return
     */
    @RequestMapping("/qndatagridByFramework")
    @ResponseBody
    public List<TenantUsers> qndatagridByFramework(HttpServletRequest request, DataGrid dataGrid, Long task_id, Long framework_id, String admin_uuid) {
        String opTime = new SimpleDateFormat("yyyyMMddHHmmssSSSS").format(new Date());
        Map<String, Object> map = new HashMap<String, Object>();
        TenantFrameworks framework = tenantFrameworksService.getFrameworkById(framework_id);
        List<Long> framework_ids = new ArrayList<>();
        if(framework != null){
            framework_ids = getFrameworkIdsByFatherId(framework_id,framework.getTenant_id());
        }
        map.put("task_id", task_id);
        map.put("framework_ids", framework_ids);
        List<TenantUsers> list = tenantUsersService.listQnTaskUsers(null,map);
        return list;
    }
}
